We’ll be using the tidycensus package to pull both census data, as well as geospatial boundaries. Let’s quickly review how we use it.
Your original .Renviron will be backed up and stored in your R HOME directory if needed.
Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY").
To use now, restart R or run `readRenviron("~/.Renviron")`
[1] "2a6f8c21a30d3024e038d67d7d4eba647dc79cd4"
Here, we choose variables we want
Code
#chose variables we wantmyvars <-c(totalpop ="B01003_001",medincome ="B19013_001",medage ="B01002_001")
Now, we pull for GA counties
Code
#pull for GA countiesga_counties_withgeo <-get_acs(geography ="county",variables =c(myvars),state ="GA",output ="wide",geometry =TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
Warning: Found less unique colors (9) than unique zcol values (159)!
Interpolating color vector to match number of zcol values.
Code
# makes label of county name and median income
Customize popups
Code
mypopup <- glue::glue("<strong>{ga_counties_withgeo$NAM}</strong><br /> Total Population: {ga_counties_withgeo$totalpop}<br /> Median Income: {ga_counties_withgeo$medincome}") %>%lapply(htmltools::HTML)# mylabel <- glue::glue("{all_data$State} {all_data$PctChange10_20}%") %>% lapply(htmltools::HTML)
Code
head(mypopup)
[[1]]
<strong>Bibb County, Georgia</strong><br />
Total Population: 156711<br />
Median Income: 43862
[[2]]
<strong>Charlton County, Georgia</strong><br />
Total Population: 12416<br />
Median Income: 45494
[[3]]
<strong>Treutlen County, Georgia</strong><br />
Total Population: 6410<br />
Median Income: 35441
[[4]]
<strong>Wheeler County, Georgia</strong><br />
Total Population: 7568<br />
Median Income: 26776
[[5]]
<strong>Toombs County, Georgia</strong><br />
Total Population: 26956<br />
Median Income: 42975
[[6]]
<strong>Coweta County, Georgia</strong><br />
Total Population: 144928<br />
Median Income: 83486
Warning: Found less unique colors (9) than unique zcol values (159)!
Interpolating color vector to match number of zcol values.
Source Code
---title: "Analysis Walkthrough Example"author: "Riya Sharma, based on a lecture by Aaron Kessler"format: html: self-contained: true code-fold: true code-tools: true---```{r setup, include=FALSE}knitr::opts_chunk$set(echo =TRUE)library(tidyverse)library(tigris)library(sf)library(tidycensus)library(htmltools)library(janitor)library(here)library(mapview)library(leafsync)library(leaflet.extras2)options(tigris_class ="sf")```# Interactive Maps with MapView PackageWe'll be using the tidycensus package to pull both census data, as well as geospatial boundaries. Let's quickly review how we use it.```{r echo=FALSE}# uncomment to run, then recomment it out so you don't run it every timecensus_api_key("2a6f8c21a30d3024e038d67d7d4eba647dc79cd4", install=TRUE, overwrite =TRUE)```Here, we choose variables we want```{r}#chose variables we wantmyvars <-c(totalpop ="B01003_001",medincome ="B19013_001",medage ="B01002_001")```Now, we pull for GA counties```{r}#pull for GA countiesga_counties_withgeo <-get_acs(geography ="county",variables =c(myvars),state ="GA",output ="wide",geometry =TRUE)ga_counties_withgeo```We can also get all counties in the US```{r}#all counties in the USall_counties_withgeo <-get_acs(geography ="county",variables =c(myvars),output ="wide",geometry =TRUE)all_counties_withgeo```Then, we remove MOE columns - they all end with "M"```{r}#remove MOE columns - they all end with "M"ga_counties_withgeo <- ga_counties_withgeo %>%select(-ends_with("M"))ga_counties_withgeo```...and also remove that trailing "E"```{r}#remove that trailing "E"colnames(ga_counties_withgeo) <-sub("E$", "", colnames(ga_counties_withgeo)) # $ means end of string onlyga_counties_withgeo```Mapping GA counties with mapview```{r}mapview(ga_counties_withgeo, zcol ="medincome")mapview(ga_counties_withgeo, zcol ="medage")```Customize colors```{r}mapview(ga_counties_withgeo, zcol ="medincome", col.regions = RColorBrewer::brewer.pal(9, "Greens"), alpha.regions =1)```This map’s dark background appeared automatically, because mapview determined the map included a lot of light colors. You can turn off that feature.```{r}mapviewOptions("basemaps.color.shuffle"=FALSE)``````{r}mapview(ga_counties_withgeo, zcol ="medincome", col.regions = RColorBrewer::brewer.pal(9, "Greens"), alpha.regions =1)```Two maps together```{r}map_income <-mapview(ga_counties_withgeo, zcol ="medincome", col.regions = RColorBrewer::brewer.pal(9, "Greens"), alpha.regions =1)map_age <-mapview(ga_counties_withgeo, zcol ="medage", col.regions = RColorBrewer::brewer.pal(9, "Greens"), alpha.regions =1)``````{r}# two maps togethersync(map_income, map_age)```Side-by-side slider to compare the two, from the leaflet.extras2 package ```{r}map_income | map_age```To turn off legends, hover text, popups```{r}mapview(ga_counties_withgeo, zcol ="medincome", col.regions = RColorBrewer::brewer.pal(9, "Greens"), alpha.regions =1,legend =FALSE, label =FALSE, popup =FALSE)```Customize labels with the glue package```{r}mylabel <- glue::glue("{ga_counties_withgeo$NAM} {ga_counties_withgeo$medincome}")mapview(ga_counties_withgeo, zcol ="medincome", col.regions = RColorBrewer::brewer.pal(9, "Greens"), alpha.regions =1,label = mylabel)# makes label of county name and median income```Customize popups```{r}mypopup <- glue::glue("<strong>{ga_counties_withgeo$NAM}</strong><br /> Total Population: {ga_counties_withgeo$totalpop}<br /> Median Income: {ga_counties_withgeo$medincome}") %>%lapply(htmltools::HTML)# mylabel <- glue::glue("{all_data$State} {all_data$PctChange10_20}%") %>% lapply(htmltools::HTML)``````{r}head(mypopup)``````{r}mapview(ga_counties_withgeo, zcol ="medincome", col.regions = RColorBrewer::brewer.pal(9, "Greens"), alpha.regions =1,popup = mypopup,label = mylabel)```